package com.example.fengyungo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.fengyungo.constant.LoginConstant;
import com.example.fengyungo.domain.pojo.LoginUser;
import com.example.fengyungo.result.Result;
import com.example.fengyungo.service.LoginService;
import com.example.fengyungo.util.RedisCache;
import com.example.fengyungo.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    RedisCache redisCache;

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return Result
     */
    @Override
    public Result login(String username, String password) {
        // AuthenticationManager authenticate进行用户认证
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(username, password);
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);

        // 使用userid生成token
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();

        Integer userType = loginUser.getUserType();

        String id = null;
        HashMap<String, String> map = new HashMap<>();
        // 用户为管理员
        if (userType.equals(LoginConstant.ADMIN_TYPE)) {
            id = loginUser.getAdmin().getAdminId();
            map.put("userInfo", JSONObject.toJSONString(loginUser.getAdmin()));
        }
        // 用户为司机
        else if (userType.equals(LoginConstant.DRIVER_TYPE)) {
            id = loginUser.getDriver().getId();
            map.put("userInfo", JSONObject.toJSONString(loginUser.getDriver()));
        }
        // 普通用户
        else if (userType.equals(LoginConstant.USER_TYPE)) {
            id = loginUser.getUser().getUserId();
            map.put("userInfo", JSONObject.toJSONString(loginUser.getUser()));
        }

        String jwt = TokenUtil.genAccessToken(id);
        log.info("jwt=" + jwt);
        // authenticate存入redis
        redisCache.setCacheObject("login:" + id, loginUser);
        // 把token响应给前端
        map.put("token", jwt);

        return Result.success(map);
    }

    @Override
    public Result logout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userid = loginUser.getAdmin().getAdminId();
        redisCache.deleteObject("login:" + userid);
        return Result.success();
    }
}
